var activePage = "monitoring";
var agents = [];

$(document).ready(function() {
    $("#accordion").accordion();
});

var timeoutHandler = function() {
    var d = arguments.callee;

    $.getJSON("refresh", function(data) {
        var actualAgents = [];
        $.each(data, function(index, ping) {
            actualAgents.push(ping.sourceId);
        });

        // remove dead agents
        $.each(agents, function(index, agent) {
            if(actualAgents.indexOf(agent) < 0) {
                $('#A' + agent + 'Label').remove();
                $('#A' + agent + 'Content').remove();
                $("#accordion").accordion();
            }
        });

        // show new agents
        $.each(actualAgents, function(index, agent) {
            if((agents.indexOf(agent) < 0) && ($('#A' + agent + 'Label').size() == 0)) {
                $.get('renderAgentInfo', { agentId: agent }, function(data) {
                    $("#accordion").append(data);
                    $("#accordion").accordion('destroy').accordion();
                });
            }
        });

        // refresh agents
        $.each(data, function(index, ping) {
            var id = ping.sourceId
            $('#A' + id + 'Status').html(ping.agentStatus.name);
            $('#A' + id + 'Description').html(ping.agentStatus.description);
            if(ping.taskSummary != null) {
                $('#A' + id + 'TaskName').html(ping.taskSummary.name);
                $('#A' + id + 'TaskDescription').html(ping.taskSummary.description);
            }
            $('#A' + id + 'JmxUrl').html(ping.jmxUrl);
            $('#A' + id + 'FreeMemory').html(ping.vmSummary.freeMemory + ' (bytes)');
            $('#A' + id + 'TotalMemory').html(ping.vmSummary.totalMemory + ' (bytes)');
            $('#A' + id + 'VMName').html(ping.vmSummary.vmName);
            $('#A' + id + 'VMVersion').html(ping.vmSummary.vmVersion);
            $('#A' + id + 'VMVendor').html(ping.vmSummary.vmVendor);
            $('#A' + id + 'VMUptime').html(ping.vmSummary.vmUptime + ' (ms)');
            $('#A' + id + 'OSArch').html(ping.vmSummary.osArch);
            $('#A' + id + 'OSVersion').html(ping.vmSummary.osVersion);
            $('#A' + id + 'OSName').html(ping.vmSummary.osName);
            $('#A' + id + 'AvailableProcessors').html(ping.vmSummary.availableProcessors);
            $('#A' + id + 'HDDTotalSpace').html(ping.vmSummary.hddTotalSpace + ' (bytes)');
            $('#A' + id + 'HDDFreeSpace').html(ping.vmSummary.hddFreeSpace + ' (bytes)');
            $('#A' + id + 'LoadedClassesCount').html(ping.vmSummary.loadedClassesCount);
            $('#A' + id + 'ThreadCount').html(ping.vmSummary.threadCount);
            $('#A' + id + 'PeakThreadCount').html(ping.vmSummary.peakThreadCount);
            $('#A' + id + 'DaemonThreadCount').html(ping.vmSummary.daemonThreadCount);
        });
        setTimeout("timeoutHandler()", 1500);
        agents = actualAgents;
    });
}

setTimeout("timeoutHandler()", 1500);
